// tsc -t es6 3-1.ts && node 3-1.js
// ts-node 3-1.ts 

// 3-1声明变量
let number1 = 4;
let number2 = 2;
const number4 = 3;
// number4 = 4;

function doSomething() {
    for (let i = 0; i < 5; i++) {
        console.log(i)
    }
    // console.log("finally i=",i)
}
doSomething();

// 课程3-3: number， string, boolean
function add(n1: number, n2: number) {
    return n1 + n2;
}
console.log(add(number1,number2))

let isTrue: boolean = true;
// isTrue = "true";

let total: number = 0
let firstName: string = 'madison'
let str = `我叫${firstName}`
console.log(str)

// 课程3-4:  数组(array) 和元组(Tupple)
let list1: number[] = [1, 2, 3, 4]
let list2: Array<number> = [1, 2, 3, 4]
let list3 = [1, 2, 3, 4]

let list4 = [1, 'ddd']
// list4.push(true)
let list5: any[] = [1, 'ddd', true]
list5.push(5)

let person1: [number, string] = [1, 'madison']
// person1[0] = 'ddd'
// person1[1] = 1
// person1[2] = 111
person1.push(111)
console.log(person1)

let person2 = [1, 'madison']
person2[0] = 'ddd'
person2[1] = 1
person2[2] = 'ddd'
console.log(person2)

// 3-5 联合(Union)类型与文献(Literal)类型
let union: string | number
union = 2
union = 'madison'
// union = true

let union2: number | string | boolean | string[]
function merge(n1: number | string, n2: number | string, resultType: 'as-number' | 'as-string') {
    if (resultType == 'as-string') {
        return n1.toString() + n2.toString()
    }
    if (typeof n1 === 'string' || typeof n2 === 'string') {
        return n1.toString() + n2.toString()
    }
    return n1 + n2
}
let mergeNumber = merge(2, 5, 'as-number')
let mergeNumber2 = merge(2, 5, 'as-string')
let mergeString = merge('hello', 'world', 'as-string')
console.log(mergeNumber)
console.log(mergeNumber2)
console.log(mergeString)

// 字面量类型
let union3 = 0 | 1 | 2
union3 = 1
union3 = 4
console.log(union3)

let literal: 1 | '2' | true | [1, 2, 3, 4]

// 3-6 枚举类型 Enum
enum Color {
    red = 5,
    green,
    blue
}

enum Color2 {
    red = 5,
    green = 10,
    blue = 1
}

enum Color3 {
    red = 'red',
    green = 'green',
    blue = 1
}

let color = Color.blue
console.log(color)
let color3 = Color3.green
console.log(color3)

// 3-7 any和 unkonwn
// let randomValue:any = 666
let randomValue: unknown = 666
randomValue = true;
randomValue = 'amasd'
randomValue = {}
if (typeof randomValue === 'function') {
    randomValue()
}
if (typeof randomValue === 'string') {
    randomValue.toUpperCase()
}

// 3-8 void、undefined 与 never
function printResult(): undefined {
    console.log('llll');
    return;
}
console.log('hello', printResult())

function throwErro(message: string, errorCode: number): never {
    throw {
        message,
        errorCode
    }
}
// throwErro('not found', 404)

function whileLoop(): never {
    while (true) {
        console.log('hhh');
    }
}

// 3-9 类型适配(类型断言) Type Assertions
let message: any;
message = 'abc'
// message.endWith('c')

let ddd = (<string>message).endsWith('c')
console.log(ddd)
let ddd2 = (message as string).endsWith('c')
console.log(ddd2)

// 3-10 函数类型
let log = function (message:string) {
    console.log(message)
}
let log2 = (message: string) => console.log(message)
log2('madison')
// log2(2)

let log3 = (message: string, code: number) => {
    console.log(message, code)
}
log3('hello', 1)

let log4 = (message: string, code?: number) => {
    console.log(message, code)
}
log4('hello')

let log5 = (message: string, code: number = 0) => {
    console.log(message, code)
}
log5('hello')